package com.iwe3.operation.controller;

import com.iwe3.operation.Enum.SystemEnum;
import com.iwe3.operation.common.R;
import com.iwe3.operation.dto.PermissionDto;
import com.iwe3.operation.handler.Asserts;
import com.iwe3.operation.service.IUrlPermissionService;
import com.iwe3.operation.util.TokenUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/url")
@RestController
public class UrlPermissionController {

    private final IUrlPermissionService urlPermissionServiceImpl;

    public UrlPermissionController(IUrlPermissionService urlPermissionServiceImpl) {
        this.urlPermissionServiceImpl = urlPermissionServiceImpl;
    }

    /**
     * 获取当前用户的菜单权限列表
     * 
     * @param token 用户认证token，从请求头中获取
     * @return 返回用户拥有的菜单权限列表
     */
    @GetMapping("/menus")
    public R menus(@RequestHeader("authentication") String token) {
        Asserts.fail(!StringUtils.hasLength(token), SystemEnum.NO_POWER_ACCESS);
        token = token.replace("Bearer ", "");
        long managerId = Long.valueOf(TokenUtil.parsePayLoad(token, "sub") + "");
        List<PermissionDto> list = urlPermissionServiceImpl.getPermissionList(managerId);
        return R.ok(list);
    }
}
